<template>
  <el-dialog
    :title="!dataForm.modeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="户型名称" prop="modeName">
      <el-input v-model="dataForm.modeName" placeholder="户型名称"></el-input>
    </el-form-item>
    <el-form-item label="户型大小" prop="modeSize">
      <el-input v-model="dataForm.modeSize" placeholder="户型大小"></el-input>
    </el-form-item>
    <el-form-item label="建筑面积" prop="modeArea">
      <el-input v-model="dataForm.modeArea" placeholder="建筑面积"></el-input>
    </el-form-item>
    <el-form-item label="户型朝向" prop="modeDirection">
      <el-input v-model="dataForm.modeDirection" placeholder="户型朝向"></el-input>
    </el-form-item>
    <el-form-item label="房产价格 约N万/套" prop="modePrice">
      <el-input v-model="dataForm.modePrice" placeholder="房产价格 约N万/套"></el-input>
    </el-form-item>
    <el-form-item label="户型图" prop="modeUrl">
      <el-input v-model="dataForm.modeUrl" placeholder="户型图"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          modeId: 0,
          modeName: '',
          modeSize: '',
          modeArea: '',
          modeDirection: '',
          modePrice: '',
          modeUrl: ''
        },
        dataRule: {
          modeName: [
            { required: true, message: '户型名称不能为空', trigger: 'blur' }
          ],
          modeSize: [
            { required: true, message: '户型大小不能为空', trigger: 'blur' }
          ],
          modeArea: [
            { required: true, message: '建筑面积不能为空', trigger: 'blur' }
          ],
          modeDirection: [
            { required: true, message: '户型朝向不能为空', trigger: 'blur' }
          ],
          modePrice: [
            { required: true, message: '房产价格 约N万/套不能为空', trigger: 'blur' }
          ],
          modeUrl: [
            { required: true, message: '户型图不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.modeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.modeId) {
            this.$http({
              url: this.$http.adornUrl(`/house/housemode/info/${this.dataForm.modeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.modeName = data.houseMode.modeName
                this.dataForm.modeSize = data.houseMode.modeSize
                this.dataForm.modeArea = data.houseMode.modeArea
                this.dataForm.modeDirection = data.houseMode.modeDirection
                this.dataForm.modePrice = data.houseMode.modePrice
                this.dataForm.modeUrl = data.houseMode.modeUrl
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/housemode/${!this.dataForm.modeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'modeId': this.dataForm.modeId || undefined,
                'modeName': this.dataForm.modeName,
                'modeSize': this.dataForm.modeSize,
                'modeArea': this.dataForm.modeArea,
                'modeDirection': this.dataForm.modeDirection,
                'modePrice': this.dataForm.modePrice,
                'modeUrl': this.dataForm.modeUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
